package renderer;

import input.KeyDetector;

import renderer.RenderObject.Primitive;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Screen extends JPanel{
	private static final long serialVersionUID = 1L;
	
	public KeyDetector detector;
	
	private ArrayList<RenderObject> renderQueue;
	
	public Screen(){
		setPreferredSize(new Dimension(800, 600));
		setFocusable(true);
		requestFocus();
		setBackground(Color.BLACK);
		detector = new KeyDetector();
		addKeyListener(detector);
		renderQueue = new ArrayList<RenderObject>();
	}
	
	@Override
	public void paint(Graphics g){
		super.paint(g);
		//System.out.println("Painting!");
		Graphics2D g2d = (Graphics2D)g;
		
		for(int i = 0; i < renderQueue.size(); i++){
			//System.out.println("Drawing object number " + i);
            if(renderQueue.get(i).type == Primitive.RECT){
                int halfWidth = (int)renderQueue.get(i).width / 2;
                int halfHeight = (int)renderQueue.get(i).height / 2;
                
                g2d.setColor(renderQueue.get(i).color);
                g2d.fillRect((int)renderQueue.get(i).x - halfWidth, (int)renderQueue.get(i).y - halfHeight, (int)renderQueue.get(i).width, (int)renderQueue.get(i).height);
            }else if(renderQueue.get(i).type == Primitive.CIRCLE){
                g2d.setColor(renderQueue.get(i).color);
                g2d.fillOval((int)renderQueue.get(i).x - ((int)renderQueue.get(i).radius / 2), (int)renderQueue.get(i).y - ((int)renderQueue.get(i).radius / 2), (int)renderQueue.get(i).radius * 2, (int)renderQueue.get(i).radius * 2);
            }
		}
		renderQueue.clear();
	}
	
	public void DrawObject(RenderObject obj){
		renderQueue.add(obj);
		//System.out.println("Added to queue!");
	}
}
